﻿-- Author      : Kurapica

--------------------------
-- CheckBox		        --
--------------------------
do
	-- Check Version
	local version = 1

	if not IGAS:RegisterFile("IGAS.GUI.Widget.CheckBox", version) then
		return
	end

	local getmetatable = getmetatable
	local pairs = pairs
	local ipairs = ipairs
	local type = type
	local error = error
	local CreateFrame = IGAS.GUI.CreateFrame

	local _WidgetName = "CheckBox"
	local _Base = "Button"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
	local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType

	-- Script
    local function OnClick(self, ...)		
        self:GetChild("ChkBtn").Checked = not self:GetChild("ChkBtn").Checked
        self:Fire("OnValueChanged", self:GetChild("ChkBtn").Checked)
    end
    
    local function OnEnter(self)
        self.Parent:GetHighlightTexture():Show()
    end
    
    local function OnLeave(self)
        self.Parent:GetHighlightTexture():Hide()
    end
    
	-- ScriptType
	local _ScriptType = {
        ["OnValueChanged"] = true,
	}

	--	FuncProxy
	local _FuncProxy = {
        -- Checked
        ["SetChecked"] = function(self, flag)
            self:GetChild("ChkBtn").Checked = (flag and true) or false
        end,
        ["GetChecked"] = function(self)
            return self:GetChild("ChkBtn").Checked or false
        end,
        -- Text
        ["SetText"] = function(self, text)
            self:GetChild("Text").Text = text
        end,
        ["GetText"] = function(self)
            return self:GetChild("Text").Text
        end,
        ["Enable"] = function(self)
            self.__UI:Enable()
            self:GetChild("ChkBtn"):Enable()
        end,
        ["Disable"] = function(self)
            self.__UI:Disable()
            self:GetChild("ChkBtn"):Disable()
        end,
	}

	--	Property
	local _Property = {
        -- Checked
        ["Checked"] = {
			["Set"] = function(self, value)
				self:SetChecked(value)
			end,
			
			["Get"] = function(self)				
				return (self:GetChecked() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- Text
		["Text"] = {
			["Set"] = function(self, text)
				self:SetText(text)
                self.Width = self:GetChild("Text").Width + 26
			end,

			["Get"] = function(self)
				return self:GetText()
			end,

			["Type"] = _PropertyType.String,
		},
	}

	local function _New(parent)		
		-- New Frame
		local frame = CreateFrame("Button",nil,parent)
        
        frame.Height = 24
        frame.Width = 100
        frame.MouseEnabled = true
        
        local chkBtn = CreateFrame("CheckButton", "ChkBtn", frame)
        chkBtn:SetPoint("TOPLEFT", frame, "TOPLEFT")
        chkBtn.MouseEnabled = false
        
        local text = CreateFrame("FontString","Text",frame,"OVERLAY","GameFontNormal")
        text.JustifyH = "LEFT"
        text:SetPoint("TOPLEFT", chkBtn, "TOPRIGHT")
        text:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT")
        text.Text = ""
        
        frame:SetHighlightTexture("Interface\\PaperDollInfoFrame\\UI-Character-Tab-Highlight")
        local t = frame:GetHighlightTexture()
        t:SetBlendMode("ADD")
        t:SetAllPoints(text)
        
        chkBtn.OnEnter = OnEnter
        chkBtn.OnLeave = OnLeave        
        frame:StackScript("OnClick", OnClick)
		
		return frame
	end	

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["FuncProxy"] = _FuncProxy,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
	}

	_Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
end
